#include "lan2datetimeedit.h"
#include<QMouseEvent>
#include <QEvent>
#include<QDebug>
#include <qapplication.h>

LAN2STA
Lan2DateTimeEdit::Lan2DateTimeEdit(QWidget *parent, int offsetx, int offsety, int width, int cal_height, int head_height, int time_height, int control_height, int line_height)
    :QDateTimeEdit(parent),mh_head(head_height),mh_cal(cal_height),mh_time(time_height),mh_control(control_height),mh_line(line_height)
{
    this->setDisplayFormat("yyyy-MM-dd hh:mm:ss");
    this->setFixedSize(200,30);
    this->setDateTime(QDateTime::currentDateTime());
    this->setStyleSheet("QDateTimeEdit::up-button{width:0px;height:0px;}"
                        "QDateTimeEdit::down-button{width:0px;height:0px;}");
    mlcc=new Lan2CalCombine(width,mh_head,mh_cal,mh_time,mh_control,mh_line);//组合控件
//    mlcc->setFixedWidth(360);
    mlcc->hide();
    mlcc->setHidden(true);

    connect(mlcc,SIGNAL(sign_combinetoday()),this,SLOT(slot_combinetoday()));
    connect(mlcc,SIGNAL(sign_combineok(QDateTime)),this,SLOT(slot_combineok(QDateTime)));
    connect(mlcc,SIGNAL(sign_combineclear()),this,SLOT(slot_combineclear()));

    mainwindow=(QWidget*)parent->topLevelWidget();

    parent->installEventFilter(this);
    lineEdit()->installEventFilter(this);
//    this->installEventFilter(this);

    moffsetx=offsetx;
    moffsety=offsety;


}

void Lan2DateTimeEdit::method_moveCombineCal(int mx, int my)
{
//    qDebug()<<mx<<my<<topLevelWidget()->x()<<topLevelWidget()->y()<<this->parentWidget()->x();
    mlcc->move(mx,my);
}

void Lan2DateTimeEdit::method_update(int mx, int my)
{
    method_moveCombineCal(this->x()+mx,this->y()+my+mi_movey);
    mlcc->method_movehhplane(this->x()+mx+10,this->y()+my+310);
    mlcc->method_movemmplane(this->x()+mx+10,this->y()+my+190);
    mlcc->method_movessplane(this->x()+mx+20,this->y()+my+190);
}

void Lan2DateTimeEdit::method_delmlcc()
{

    mlcc->deleteLater();
}

void Lan2DateTimeEdit::method_delAllTimePlanes()
{
    mlcc->method_delAllplane();
}

void Lan2DateTimeEdit::method_hidemlcc()
{
    mlcc->hide();

}

void Lan2DateTimeEdit::method_showmlcc()
{
    mlcc->show();
}

QString Lan2DateTimeEdit::method_getTxt() const
{
    return this->text().simplified();
}

void Lan2DateTimeEdit::slot_combinetoday()
{
    this->setDateTime(QDateTime::currentDateTime());

}

void Lan2DateTimeEdit::slot_combineok(QDateTime dt)
{
    this->setDateTime(dt);

    mlcc->hide();
    mlcc->method_hideAllplane();
}

void Lan2DateTimeEdit::slot_combineclear()
{
//    this->clear();
//    lineEdit()->clear();
    mlcc->hide();
    mlcc->method_hideAllplane();


}



bool Lan2DateTimeEdit::eventFilter(QObject *watched, QEvent *event)
{
    if(watched==lineEdit() && event->type()==QEvent::MouseButtonPress){
        QMouseEvent* eve=(QMouseEvent*)event;
        if(eve->buttons()==Qt::LeftButton){
            mlcc->setHidden(true);

            if(mlcc->isHidden()){
              mlcc->setHidden(false);
              mlcc->show();
            }
        }
    }

    if(watched==mainwindow && event->type()==QEvent::Move){

        QApplication::sendEvent(this,event);
    }

    if(watched==mainwindow && event->type()==QEvent::MouseButtonPress){

        mlcc->hide();
        mlcc->method_hideAllplane();

    }

    if(watched==mainwindow && event->type()==QEvent::WindowStateChange){

         mlcc->hide();
         mlcc->method_hideAllplane();
    }

    if(watched==mainwindow && event->type()==QEvent::Close){
        this->method_delmlcc();
        this->method_delAllTimePlanes();


    }








    return QDateTimeEdit::eventFilter(watched,event);
}

void Lan2DateTimeEdit::moveEvent(QMoveEvent *event)
{
    method_moveCombineCal(mainwindow->x()+this->x()+moffsetx-10,mainwindow->y()+this->y()+mi_movey+moffsety-10);
    mlcc->method_movehhplane(mainwindow->x()+this->x()+10+moffsetx,mainwindow->y()+this->y()+310+moffsety);
    mlcc->method_movemmplane(mainwindow->x()+this->x()+10+moffsetx,mainwindow->y()+this->y()+190+moffsety);
    mlcc->method_movessplane(mainwindow->x()+this->x()+20+moffsetx,mainwindow->y()+this->y()+190+moffsety);
}


LAN2END

